package cn.pigeon.health.service.impl;

import cn.pigeon.health.dao.OrderSettingDao;
import cn.pigeon.health.pojo.OrderSetting;
import cn.pigeon.health.service.OrderSettingService;
import cn.pigeon.health.utils.DateUtils;
import cn.pigeon.health.utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * 预约管理服务层实现类
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 批量导入Excel数据
     * @param lstData
     */
    public void importData(List<String[]> lstData) throws IOException {
        for (String[] itemStrs : lstData) {
            if(itemStrs.length==2){
                Date orderDate = DateUtils.
                        CovertToDateFromString(itemStrs[0],"yyyy/MM/dd","yyyy-MM-dd");
                if(orderDate==null){
                    //日期转换失败,直接跳过
                    continue;
                }
                Integer number;
                try {
                    number = Integer.parseInt(itemStrs[1]);
                } catch (NumberFormatException e) {
                    //数字转换失败，直接跳过
                    continue;
                }
                //封装数据
                OrderSetting orderSetting = new OrderSetting(orderDate,number);
                //数据校验通过，插入到数据库(要支持重复导入,所以要判断是否存在，存在则更新，不存在，则添加)
                Integer countByOrderDate = orderSettingDao.findCountByOrderDate(orderDate);
                if(countByOrderDate>0){
                    orderSettingDao.edit(orderSetting);
                }else{
                    orderSettingDao.add(orderSetting);
                }
            }else{
                //长度不正确,直接跳过
                continue;
            }
        }
    }

    /**
     * 查询预约管理数据(按月查)
     * @param map
     * @return
     */
    public List<Map> queryData(Map map) {
        List<OrderSetting> lstData = orderSettingDao.queryData(map);
        List<Map> lstMap = new ArrayList<Map>();
        for (OrderSetting orderSetting : lstData) {
            Map<String,Object> mapItem = new HashMap<String,Object>();
            int dateValue = orderSetting.getOrderDate().getDate();
            mapItem.put("date",dateValue);
            mapItem.put("number",orderSetting.getNumber());
            mapItem.put("reservations",orderSetting.getReservations());
            lstMap.add(mapItem);
        }
        return lstMap;
    }

    /**
     * 设置可预约人数
     * @param orderSetting
     */
    public void editNumberByDate(OrderSetting orderSetting) {
        orderSettingDao.editNumberByDate(orderSetting);
    }

    /**
     * 根据日期查询详情
     * @param orderDate
     * @return
     */
    public OrderSetting findByDate(String orderDate) {
        return orderSettingDao.findByDate(orderDate);
    }
}
